package com.lx.jdbc;

import java.sql.*;

public class JdbcDemo2 {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        String sql = "select * from student1";

        try {
            //1. 注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2. 获取连接对象
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
            //3. 获取执行sql对象
            stmt = conn.createStatement();
            //4. 执行sql
            rs = stmt.executeQuery(sql);
            //5. 处理结果
   /*                  让游标向下移动一行
                    rs.next();
                     获取数据
                    int i = rs.getInt(1);
                    String name = rs.getString("name");
                    System.out.println(i+"---"+name);*/
            while (rs.next()) {
                int i = rs.getInt(1);
                String name = rs.getString("name");
                System.out.println(i + "---" + name);
            }
            stmt.close();
            conn.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
